home *** CD-ROM | disk | FTP | other *** search
- /***
- * Created by Bill Hubauer on Fri, Jun 21, 1996 @ 2:25 AM.
- *
- ***/
-
-
- #ifndef __BlasteroidsGame_H__
- #include "BlasteroidsGame.h"
- #endif
- #include "BlasteroidsShipSprite.h"
-
-
- CSprite* CBlasteroidsGame::MakeExtensionSprite(GWorldPtr image,RgnHandle mask) //Override
- {
- return new CBlasteroidTargetSprite(GetWorld(), this, image, mask);
- }
-
-
- OSErr CBlasteroidsGame::Initialize()//Override
- {
- OSErr err = noErr;
-
- err = inherited::Initialize();
- if(err == noErr){
- new CBlasteroidsShipSprite(GetWorld(), this);
-
- PlaySound(5000,true);
- }
-
- return err;
- }
-
-
- CBlasteroidsGame::CBlasteroidsGame()
- : CSpriteGame(5000)
- {
- }
-
-
- CBlasteroidsGame::~CBlasteroidsGame()//Override
- {
- }
-
-
- static short Abs(short value)
- {
- if (value < 0)
- return -value;
- else
- return value;
- }
-
-
- static short RandomInRange(short low, short hi)
- {
- if (hi == low)
- return low;
-
- if (hi < low)
- {
- short temp = hi;
- hi = low;
- low = temp;
- }
-
- return (Abs(Random()) % (hi - low + 1)) + low;
- }
-
-
- void CBlasteroidsGame::DrawBackground(const Rect& inBounds)
- {
- RGBColor pixel = {-1, -1, -1};
- for (short i=1; i<500; i++)
- {
- SetCPixel(RandomInRange(inBounds.left, inBounds.right),
- RandomInRange(inBounds.top, inBounds.bottom), &pixel);
- }
- }
-
-
- static short CalcBlasteroidTargetStartTop(CSpriteWorld* world)
- {
- const Rect& bounds = *(world->GetSpriteCanvas()->GetBounds());
-
- return RandomInRange(bounds.top+10, bounds.bottom-32-10);
- }
-
-
- static short CalcBlasteroidTargetStartLeft(CSpriteWorld* world)
- {
- const Rect& bounds = *(world->GetSpriteCanvas()->GetBounds());
-
- return RandomInRange(bounds.left+10, bounds.right-32-10);
- }
-
-
- CBlasteroidTargetSprite::CBlasteroidTargetSprite(CSpriteWorld* world,CSpriteGame* game,GWorldPtr image,
- RgnHandle mask)
- : CGameSprite(world, game, 0, image,
- 0, 0, mask)
- {
- const Rect& bounds = *(world->GetSpriteCanvas()->GetBounds());
-
- switch (RandomInRange(1, 3))
- {
- case 0:
- MoveTo(RandomInRange(bounds.left, bounds.left+100),
- RandomInRange(bounds.top, bounds.top+100));
- break;
- case 1:
- MoveTo(RandomInRange(bounds.right-100, bounds.right),
- RandomInRange(bounds.top, bounds.top+100));
- break;
- case 2:
- MoveTo(RandomInRange(bounds.left, bounds.left+100),
- RandomInRange(bounds.bottom-100, bounds.bottom));
- break;
- default:
- MoveTo(RandomInRange(bounds.right-100, bounds.right),
- RandomInRange(bounds.bottom-100, bounds.bottom));
- break;
- }
-
- fDeltaH = RandomInRange(-10, 10);
- fDeltaV = RandomInRange(-10, 10);
- }
-
-
- CBlasteroidTargetSprite::~CBlasteroidTargetSprite()
- {
- }
-
-
- void CBlasteroidTargetSprite::UpdatePosition() //Override
- {
- MoveBy(fDeltaH,fDeltaV);
-
- if (fLocation.left < GetGameBounds()->left)
- MoveToH(GetGameBounds()->right);
- if (fLocation.left > GetGameBounds()->right)
- MoveToH(GetGameBounds()->left);
- if (fLocation.top < GetGameBounds()->top)
- MoveToV(GetGameBounds()->bottom);
- if (fLocation.top > GetGameBounds()->bottom)
- MoveToV(GetGameBounds()->top);
- }
-
-
- Boolean CBlasteroidTargetSprite::WasHitBy(CSprite* hitByThis)
- {
- CBlasteroidsBulletSprite* bullet = dynamic_cast<CBlasteroidsBulletSprite*>(hitByThis);
- if (bullet != nil)
- {
- PlaySound(5002);
- delete bullet;
- delete this;
- return false;
- }
-
- CBlasteroidsShipSprite* ship = dynamic_cast<CBlasteroidsShipSprite*>(hitByThis);
- if (ship != nil)
- {
- PlaySound(5003);
- //delete ship;
- return false;
- }
-
- return true;
- }
-
-
-
-